import 'package:hongsheng/api/SPClassApiManager.dart';
import 'package:hongsheng/api/SPClassHttpCallBack.dart';
import 'package:hongsheng/app/SPClassApplicaion.dart';
import 'package:hongsheng/model/AiRecordEntity.dart';
import 'package:hongsheng/pages/common/SPClassNoDataView.dart';
import 'package:hongsheng/utils/SPClassCommonMethods.dart';
import 'package:hongsheng/utils/SPClassImageUtil.dart';
import 'package:hongsheng/utils/colors.dart';
import 'package:hongsheng/widgets/SPClassBallFooter.dart';
import 'package:hongsheng/widgets/SPClassBallHeader.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'SPClassAiPage.dart';


///战绩盘点
class SPClassPanDian extends StatefulWidget {
  const SPClassPanDian({Key ?key}) : super(key: key);

  @override
  _SPClassPanDianState createState() => _SPClassPanDianState();
}

class _SPClassPanDianState extends State<SPClassPanDian> {
  EasyRefreshController ?_controller ;
  int page =1;
  List<AiRecordEntity> dataList =[];
  var myEventBus;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _controller =new EasyRefreshController();
    spFunOnRefresh();
    myEventBus=SPClassApplicaion.spProEventBus.on<String>().listen((event) {
      if(event.startsWith("aiRefresh:")){
        spFunOnRefresh();
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return EasyRefresh.custom(
      topBouncing: false,
      footer: SPClassBallFooter(
        textColor: Color(0xFF8F8F8F),
      ),
      header: SPClassBallHeader(
        textColor: Color(0xFF8F8F8F),
      ),
      controller:_controller,
      onRefresh: spFunOnRefresh,
      onLoad: spFunOnMore,
      slivers: <Widget>[
        SliverList(
          delegate: SliverChildListDelegate(
              [
                dataList.length == 0 ? SPClassNoDataView(height: width(400),): Container()
              ]
          ),
        ),
        SliverList(
          delegate: SliverChildBuilderDelegate((c,i){
            return itemWidget(i);
          },
              childCount:dataList.length
          ),
        ),
      ],
    );
  }

  Widget itemWidget(int index){
    AiRecordEntity data = dataList[index];
    return Container(
      decoration: BoxDecoration(
          color: Colors.white,
          border: Border(bottom: BorderSide(width: 0.4,color: Colors.grey[300]!))
      ),
      child: Row(
        children: [
          Container(
            width: width(50),
            height: height(48),
            alignment: Alignment.center,
            child: (index<3&&index>=0) ?
            buildMedal(index+1):Text((index+1).toString(),style: TextStyle(fontSize: sp(13),color: Color(0xFF999999)),),
          ),
          // SizedBox(width: width(10),),
          // SPClassImageUtil.spFunNetWordImage(
          //     placeholder: "",
          //     url:data.spProIconUrl,
          //     width: width(36),
          //     height:  width(36),
          // ),
          // SizedBox(width: width(10),),
          Expanded(
            child: Text('${data.spProLeagueName}',style: TextStyle(fontSize: sp(13),),overflow: TextOverflow.ellipsis,maxLines: 1,),
          ),
          SizedBox(width: width(10),),
          Container(
              width: width(70),
              alignment: Alignment.centerLeft,
              child: Text('${data.spProGuessMatchNum}中${data.spProHitNum}',style: TextStyle(fontSize: sp(13),color: MyColors.main1),)),
          Container(
              width: width(70),
              alignment: Alignment.centerLeft,
              child: Text('胜率${data.spProCorrectRate}%',style: TextStyle(fontSize: sp(13),color: MyColors.main1),)),

        ],
      ),
    );
  }

  Future spFunOnRefresh()async{
    page=1;
    dataList.clear();
    setState(() {
    });
    var params = {
      'page':page,
      'match_type':SPClassAIPageState.matchType
    };
    SPClassApiManager.spFunGetInstance().spFunAiRecord<AiRecordEntity>(params:params,spProCallBack:SPClassHttpCallBack(
        spProOnSuccess: (result){
          dataList = result.spProDataList;
          _controller?.finishRefresh(noMore: false,success: true);
          _controller?.resetLoadState();
          setState(() {
          });
        },onError: (e){
      _controller?.finishRefresh(success: false);

    }
    ));
  }

  Future spFunOnMore()async{
    page++;
    var params = {
      'page':page,
    };
    SPClassApiManager.spFunGetInstance().spFunAiRecord<AiRecordEntity>(params:params,spProCallBack:SPClassHttpCallBack(
        spProOnSuccess: (result){
          dataList.addAll(result.spProDataList);
          if(dataList.isEmpty){
            _controller?.finishLoad(noMore: true);
          }
          setState(() {
          });
        },onError: (e){
      _controller?.finishLoad(noMore: false);
    }
    ));
  }

  buildMedal(int ranking) {
    return Image.asset(
      SPClassImageUtil.spFunGetImagePath(ranking==1? "ic_leaderbord_one":(ranking==2? "ic_leaderbord_two":"ic_leaderbord_three")),
      width: width(25),
    );

  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    myEventBus.cancel();
  }
}
